﻿Imports System
Imports System.Text
Imports System.Linq
Imports System.Xml
Imports System.Text.RegularExpressions

Public Class clsXMLCore
    Public Function GetXMLTree(ByVal strTree As String) As String
        Dim Answer As String = ""
        Dim output As StringBuilder = New StringBuilder()
        Dim St As Stack = New Stack()
        Dim Q As Queue = New Queue()
        Dim ArrTree() As String = strTree.Split("(")

        For I As Integer = 0 To ArrTree.Length - 1
            Dim Cell As String = ArrTree(I)
            If Cell.Contains(")") = False Then '.TOP
                Dim X As String = Cell.Trim()
                Q.Enqueue("<" & X.ToString & ">")
                St.Push("</" & X.ToString & ">")
            Else 'PRP I))
                Dim D As String = Cell.Replace(")", "") 'PRP I
                Dim A() As String = D.Split(" ") '(PRP) (I)
                If (A(0) <> "." And A(0) <> ",") Then
                    Q.Enqueue("<" & A(0).ToString & ">" & A(1).ToString & "</" & A(0).ToString & ">")
                End If
            End If
        Next I

        Dim C As String
        For Each C In Q
            output.Append(C.ToString)
        Next C

        Dim CC As String
        For Each CC In St
            output.Append(CC.ToString)
        Next CC

        output.Remove(0, 2)
        output.Replace("</>", "")
        output.Replace("$", "1") 'to escape the $ sign. The (&#36;) also return error

        Answer = "<?xml version=""1.0""?>" & output.ToString

        Return Answer
    End Function

    Public Function getNodValue(ByVal NodeName As String, ByVal XMLString As String) As String
        Dim Answer As String = ""
        Dim XLTree As New Xml.XmlDocument
        XLTree.LoadXml(XMLString)
        Dim NList As New ListView
        Dim NVal As New ListView
        Dim d As XmlParserContext
        Dim X As New Xml.XmlTextReader(XMLString, XmlNodeType.Document, d)

        While X.Read
            If X.NodeType = Xml.XmlNodeType.Element Then
                Dim Name As String = X.Name.ToString
                If Name = NodeName Then
                    Answer = X.Value.ToString
                End If
            End If
        End While

        Return Answer
    End Function
End Class
